package com.kgc.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.novel.common.Res;
import com.kgc.novel.dao.BookIncomeMapper;
import com.kgc.novel.dao.BookRecordMapper;
import com.kgc.novel.domain.entity.BookIncome;
import com.kgc.novel.domain.entity.BookRecord;
import com.kgc.novel.domain.param.BookIncomeParam;
import com.kgc.novel.domain.vo.BookIncomeVO;
import com.kgc.novel.domain.vo.BookRecordVO;
import com.kgc.novel.service.user.BookIncomeService;
import com.kgc.novel.util.SnowflakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class BookIncomeServiceImpl extends ServiceImpl<BookIncomeMapper, BookIncome> implements BookIncomeService {
    @Resource
    private BookIncomeMapper bookIncomeMapper;

    @Override
    public Res selectIncomeAll(BookIncomeParam bookIncomeParam) {
        List<BookIncomeVO> allShelf = bookIncomeMapper.getAllIncome(bookIncomeParam);
        Integer countShelf = bookIncomeMapper.getCountIncome(bookIncomeParam.getNickName(), bookIncomeParam.getIncomeEvent(), bookIncomeParam.getCreateTime());
        return Res.returnData(allShelf, (long) countShelf);
    }

    @Override
    public Res getSingleIncome(String id) {
        BookIncome bookRecord = bookIncomeMapper.selectById(id);
        if (bookRecord != null) {
            return Res.returnData("200", bookRecord);
        }
        return Res.returnMsg("暂无用户阅读记录！");
    }

    @Override
    public Res addIncome(BookIncome bookIncome) {

        LambdaQueryWrapper<BookIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookIncome::getUserId, bookIncome.getUserId());

        BookIncome bookIncome1 = bookIncomeMapper.selectOne(lambdaQueryWrapper);
        if (bookIncome1 != null) {
            return Res.returnMsg("收益记录已经存在，请重新添加！");
        }
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        long id = idWorker.nextId();
        bookIncome.setId(String.valueOf(id));
        bookIncomeMapper.insert(bookIncome);
        return Res.returnMsg("添加成功！");
    }

    @Override
    public Res updateIncome(BookIncome bookIncome) {
        LambdaQueryWrapper<BookIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookIncome::getUserId, bookIncome.getUserId());

        BookIncome bookIncome1 = bookIncomeMapper.selectOne(lambdaQueryWrapper);
        if (bookIncome1 == null) {
            bookIncomeMapper.updateById(bookIncome);
            return Res.returnMsg("修改成功！");
        }
        BookIncome bookIncome2 = bookIncomeMapper.selectById(bookIncome.getId());
        if (bookIncome2.getId().equals(bookIncome.getId())) {
            bookIncomeMapper.updateById(bookIncome);
            return Res.returnMsg("修改成功！");
        }
        return Res.returnMsg("用户记录已经存在，请重新修改！");
    }

    @Override
    public Res deleteIncome(String id) {
        int i = bookIncomeMapper.deleteById(id);
        if (i != -1) {
            return Res.returnMsg("删除成功！");
        } else {
            return Res.returnMsg("删除失败！");
        }
    }

    @Override
    public Res deleteAllIncome(List<String> ids) {
        int i = bookIncomeMapper.deleteBatchIds(ids);
        if (i != -1) {
            return Res.returnMsg("批量删除成功");
        } else {
            return Res.returnMsg("批量删除失败");
        }
    }
}
